#include "MainSceneWidget.h"
#include "QD3D/QD3DUtils.h"
#include <glm/gtx/transform.hpp>
#include <QMouseEvent>
#include <QKeyEvent>

MainSceneWidget::MainSceneWidget(int argc, char *argv[], QWidget *parent)
		: QD3DWidget(parent)
{
 	_parseCommandLineArgs (argc, argv);
}

MainSceneWidget::~MainSceneWidget ()
{
}

void MainSceneWidget::_parseCommandLineArgs (int argc, char *argv[])
{
	// exe intrinsic_file export_extrinsic_file
	if (argc != 3)
		throw std::exception ("MainSceneWidget::_parseCommandLineArgs(): "
				"Command line argument error.");

	_reconstructProcess.addSensor (_contextManager.createSensor (
			"Sensor", argv[1]));
	_reconstructProcess.setFile (argv[2]);
}

void MainSceneWidget::initializeD3D ()
{
	QD3DUtils d3DUtils;
	_reconstructProcess.initialize ();
	_contextManager.start ();
}

void MainSceneWidget::paintD3D ()
{
	_contextManager.update ();
	if (!_reconstructProcess.grabNewFrameAndProcess ())
			close ();
	update ();
}

void MainSceneWidget::resizeD3D (int width, int height)
{
}

void MainSceneWidget::mousePressEvent (QMouseEvent * e)
{
}

void MainSceneWidget::mouseMoveEvent (QMouseEvent * e)
{
}

void MainSceneWidget::mouseReleaseEvent (QMouseEvent * e)
{
}

void MainSceneWidget::wheelEvent (QWheelEvent * e)
{
}

void MainSceneWidget::keyPressEvent (QKeyEvent * e)
{
	switch (e->key ()) {
	case Qt::Key_Escape:
		close ();
	}
}

void MainSceneWidget::keyReleaseEvent (QKeyEvent * e)
{

}

void MainSceneWidget::cleanD3D ()
{
	_reconstructProcess.clean ();
}